<?php

/**
 * Profile
 * @author ReGeDa
 * @events :home :volatile_data
 */
class Profile_Handler extends DB_Event_Handler {

	/**
	 * Constructor
	 */
	public function  __construct() {
		parent::__construct();
		platcode()->lang->load('account');
	}

	/**
	 * Show home page
	 * @param string $username
	 */
	public function on_home($username) {

		// Only for session
		if (is_user_session()) {

			$AQ = &platcode()->plugins->import('ActiveQuery');
			/* @var $AQ ActiveQuery */

			$stmt = $this->db->prepare(
				$AQ
					->set(array(
						'username' => $username,
						'timeout' => config('untrust_email_life'),
						'session_id' => SESSION_ID
					))
					->call('person')
			);
			/* @var $stmt PDOStatement */

			// query
			$AQ->query($stmt);

			// fetch a user bean
			$person = $stmt->fetch(PDO::FETCH_ASSOC);

			// is user exist?
			if ( ! empty($person['id'])) {
				// set variables
				platcode()->raw['person'] = $person;
			}
			else
				throw new Platcode_Exception(lang('no_user_found'), 404);
		}
		// Check existed user
		else {
			// set variables
			platcode()->raw['session-error'] = array(
				'name' => $username,
				'err-msg' => lang('no_display')
			);
		}

		platcode()->raw['openid-delegate'] = $username;
		platcode()->raw['page-title'] = $username;

		// import javascript
		platcode()->plugins->import('JavaScript')
			->lang('global-lang')
			->lib('p-common');

		// Output
		$this->output->prepare_xslt('profile/home');
	}

	/**
	 * Fetch volatile data
	 */
	public function on_volatile_data() {
		
		$AQ = &platcode()->plugins->import('ActiveQuery');
		/* @var $AQ ActiveQuery */

		$stmt = $this->db->prepare(
			$AQ
				->set('id', SESSION_ID)
				->call('person_volatile')
		);
		/* @var $stmt PDOStatement */

		// query
		$AQ->query($stmt);

		// fetch a user bean
		// set variables
		platcode()->raw['person'] = $stmt->fetch(PDO::FETCH_ASSOC);
	}
}

// end of file